<template>
	<view class="edit-notice-modal" v-if="visible">
		<view class="edit-mask" @click="handleClose"></view>
		<view class="edit-content">
			<view class="modal-header">
				<text class="modal-title">{{ title }}</text>
				<view style="display: flex;">
					<button class="create-btn" @click="handleSave">保存</button>
					<button v-if="showDelete" class="create-btn" @click="handleDelete" style="color: red;">删除</button>
				</view>
			</view>

			<view class="form-content">
				<!-- 开播时间 -->
				<view class="form-item" @click="openTimePicker">
					<text class="item-label">开播时间*</text>
					<view class="item-value">
						<text class="time-text" :class="{placeholder: !formData.time}">
							{{ formData.time || '请选择' }}
						</text>
						<uni-icons type="right" size="20"></uni-icons>
					</view>
				</view>

				<!-- 预告封面 -->
				<view class="form-item">
					<text class="item-label">预告封面</text>
					<view class="cover-upload" @click="chooseCover">
						<image v-if="formData.cover" :src="formData.cover" class="cover-image" mode="aspectFill">
						</image>
						<view v-else class="upload-placeholder">
							<text class="plus-icon">+</text>
						</view>
					</view>
				</view>

				<!-- 直播介绍 -->
				<view class="form-item">
					<text class="item-label">直播介绍</text>
					<textarea v-model="formData.introduction" class="intro-textarea" :placeholder="introPlaceholder"
						:maxlength="maxLength"></textarea>
					<text
						class="word-count">{{ formData.introduction ? formData.introduction.length : 0 }}/{{ maxLength }}</text>
				</view>
			</view>

			<!-- 时间选择器弹框 -->
			<view v-if="showTimePicker" class="time-picker-modal">
				<view class="picker-mask" @click="cancelTimePicker"></view>
				<view class="picker-content">
					<view class="picker-header">
						<text class="picker-title">选择开播时间</text>
					</view>
					<picker-view :value="pickerValue" @change="handleTimeChange" class="time-picker"
						indicator-style="height: 80rpx;">
						<picker-view-column>
							<view v-for="(item,index) in years" :key="index" class="picker-item">{{item}}年</view>
						</picker-view-column>
						<picker-view-column>
							<view v-for="(item,index) in months" :key="index" class="picker-item">{{item}}月</view>
						</picker-view-column>
						<picker-view-column>
							<view v-for="(item,index) in days" :key="index" class="picker-item">{{item}}日</view>
						</picker-view-column>
						<picker-view-column>
							<view v-for="(item,index) in hours" :key="index" class="picker-item">{{item}}时</view>
						</picker-view-column>
						<picker-view-column>
							<view v-for="(item,index) in minutes" :key="index" class="picker-item">{{item}}分</view>
						</picker-view-column>
					</picker-view>
					<view class="picker-buttons">
						<button class="picker-btn cancel" @click="cancelTimePicker">取消</button>
						<button class="picker-btn confirm" @click="confirmTimePicker">确定</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'EditNoticeModal',
		props: {
			visible: {
				type: Boolean,
				default: false
			},
			title: {
				type: String,
				default: '编辑直播预告'
			},
			showDelete: {
				type: Boolean,
				default: true
			},
			introPlaceholder: {
				type: String,
				default: '直播介绍可以更好的帮助观众了解您的直播内容'
			},
			maxLength: {
				type: Number,
				default: 100
			},
			initialData: {
				type: Object,
				default: () => ({
					time: '',
					title: '',
					cover: '',
					introduction: '',
					status: '未开始'
				})
			}
		},
		data() {
			const now = new Date()
			const currentYear = now.getFullYear()
			const currentMonth = now.getMonth() + 1
			const currentDay = now.getDate()
			const currentHour = now.getHours()
			const currentMinute = now.getMinutes()

			return {
				formData: JSON.parse(JSON.stringify(this.initialData)),
				showTimePicker: false,
				pickerValue: [5, currentMonth - 1, currentDay - 1, currentHour, currentMinute],
				years: Array.from({
					length: 11
				}, (_, i) => currentYear - 5 + i),
				months: Array.from({
					length: 12
				}, (_, i) => i + 1),
				days: Array.from({
					length: 31
				}, (_, i) => i + 1),
				hours: Array.from({
					length: 24
				}, (_, i) => i),
				minutes: Array.from({
					length: 60
				}, (_, i) => i),
				tempTime: ''
			}
		},
		watch: {
			initialData: {
				handler(newVal) {
					this.formData = JSON.parse(JSON.stringify(newVal))
				},
				deep: true,
				immediate: true
			}
		},
		methods: {
			handleClose() {
				this.$emit('close')
			},

			handleSave() {
				if (!this.formData.time) {
					uni.showToast({
						title: '请选择开播时间',
						icon: 'none'
					})
					return
				}

				this.$emit('save', this.formData)
			},

			handleDelete() {
				uni.showModal({
					title: '提示',
					content: '确定要删除这条直播预告吗？',
					success: (res) => {
						if (res.confirm) {
							this.$emit('delete')
						}
					}
				})
			},

			chooseCover() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: (res) => {
						this.formData.cover = res.tempFilePaths[0]
					}
				})
			},

			openTimePicker() {
				this.showTimePicker = true
				if (this.formData.time) {
					try {
						const timeArr = this.formData.time.match(/(\d+)/g)
						if (timeArr && timeArr.length >= 4) {
							const currentYear = new Date().getFullYear()
							this.pickerValue = [
								this.years.indexOf(currentYear) || 5,
								parseInt(timeArr[0]) - 1 || 0,
								parseInt(timeArr[1]) - 1 || 0,
								parseInt(timeArr[2]) || 0,
								parseInt(timeArr[3]) || 0
							]
						}
					} catch (e) {
						console.error('时间解析错误:', e)
					}
				}
			},

			cancelTimePicker() {
				this.showTimePicker = false
			},

			confirmTimePicker() {
				this.formData.time = this.tempTime || this.formatPickerTime(this.pickerValue)
				this.showTimePicker = false
			},

			handleTimeChange(e) {
				const val = e.detail.value
				this.tempTime = this.formatPickerTime(val)
			},

			formatPickerTime(value) {
				const val = value || this.pickerValue
				return `${this.months[val[1]]}月${this.days[val[2]]}日 ${this.addZero(this.hours[val[3]])}:${this.addZero(this.minutes[val[4]])}`
			},

			addZero(num) {
				return num < 10 ? `0${num}` : num
			}
		}
	}
</script>

<style lang="scss" scoped>
	.edit-notice-modal {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 1002;

		.edit-mask {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(0, 0, 0, 0.5);
		}

		.edit-content {
			position: absolute;
			bottom: 0;
			left: 0;
			width: 100%;
			background-color: #fff;
			border-radius: 24rpx 24rpx 0 0;
			max-height: 80vh;
			overflow-y: auto;

			.modal-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 30rpx;
				border-bottom: 1rpx solid #f0f0f0;

				.modal-title {
					font-size: 34rpx;
					font-weight: bold;
					color: #333;
				}

				.create-btn {
					background: none;
					border: none;
					color: #55aaff;
					font-size: 32rpx;
					padding: 0;
					margin: 0;
					line-height: 1;
					margin-left: 20rpx;

					&::after {
						border: none;
					}
				}
			}

			.form-content {
				padding: 30rpx;
			}

			.form-item {
				margin-bottom: 40rpx;

				.item-label {
					display: block;
					font-size: 30rpx;
					color: #333;
					margin-bottom: 20rpx;
					font-weight: 500;
				}

				.item-value {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 20rpx 0;
					border-bottom: 1rpx solid #f0f0f0;

					.time-text {
						font-size: 28rpx;
						color: #333;

						&.placeholder {
							color: #999;
						}
					}
				}

				.cover-upload {
					width: 200rpx;
					height: 200rpx;
					background-color: #f7f7f7;
					border-radius: 8rpx;
					display: flex;
					align-items: center;
					justify-content: center;

					.cover-image {
						width: 100%;
						height: 100%;
						border-radius: 8rpx;
					}

					.upload-placeholder {
						text-align: center;

						.plus-icon {
							font-size: 60rpx;
							color: #ccc;
							line-height: 1;
						}
					}
				}

				.intro-textarea {
					width: 100%;
					height: 200rpx;
					background-color: #f7f7f7;
					border-radius: 8rpx;
					padding: 20rpx;
					box-sizing: border-box;
					font-size: 28rpx;
					color: #333;
				}

				.word-count {
					display: block;
					text-align: right;
					font-size: 24rpx;
					color: #999;
					margin-top: 10rpx;
				}
			}
		}

		/* 时间选择器样式 */
		.time-picker-modal {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			z-index: 1003;

			.picker-mask {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				background-color: rgba(0, 0, 0, 0.5);
			}

			.picker-content {
				position: absolute;
				bottom: 0;
				left: 0;
				width: 100%;
				background-color: #fff;
				border-radius: 24rpx 24rpx 0 0;
				padding: 20rpx;
			}

			.picker-header {
				text-align: center;
				padding: 20rpx 0;

				.picker-title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
				}
			}

			.time-picker {
				height: 400rpx;
			}

			.picker-item {
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 28rpx;
				color: #333;
			}

			.picker-buttons {
				display: flex;
				justify-content: space-between;
				margin-top: 20rpx;

				.picker-btn {
					flex: 1;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					font-size: 30rpx;
					background: none;
					border: none;
					padding: 0;
					margin: 0;

					&::after {
						border: none;
					}

					&.cancel {
						color: #999;
					}

					&.confirm {
						color: #55aaff;
					}
				}
			}
		}
	}
</style>